package com.dididoctor.doctor.Activity.Patient.PatientSearch;

import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Activity.Patient.Patient;
import com.dididoctor.doctor.Activity.Patient.PatientAdapter;
import com.dididoctor.doctor.Activity.Patient.PatlentPresenter;
import com.dididoctor.doctor.Activity.Patient.PatlentView;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Ui.customlistview.OnRefreshListener;
import com.dididoctor.doctor.Ui.customlistview.RefreshListView;
import com.dididoctor.doctor.Utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： PatientSearchActivity
 * @包名称： com.dididoctor.doctor.Activity.Patient.PatientSearch
 * @创建人： zhanjunjie
 * @创建时间：2016/4/21 16:12
 * @修改人：
 * @修改时间：2016/4/21 16:12
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class PatientSearchActivity extends EduActivity implements PatlentView {
    private EditText mEtSearch;
    private Button mBtSearch;
    private RefreshListView listView;

    private PatlentPresenter presenter;
    private List<Patient> patients = new ArrayList<>();
    private PatientAdapter adapter;
    private int pageIndex = 1;
    private String name = "";

    @Override
    public void init() {
        setText(R.id.tv_title, "搜索患者");
        mEtSearch = (EditText) findViewById(R.id.ed_search);
        mBtSearch = (Button) findViewById(R.id.btn_search);
        listView = (RefreshListView) findViewById(R.id.listview);

        mBtSearch.setOnClickListener(this);
        adapter = new PatientAdapter(this, patients, "3");
        listView.setAdapter(adapter);
        listView.setHeadAndFoot(false, false);
        listView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                name = mEtSearch.getText().toString();
                if (Util.isEmpty(name)) {
                    showToastMessage("请填写关键字");
                } else {
                    presenter.getpatient(pageIndex, name, "3");
                }
            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
                presenter.getpatient(pageIndex, name, "3");

            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

        presenter = new PatlentPresenter(this, this);
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.btn_search:
                name = mEtSearch.getText().toString();
                if (Util.isEmpty(name)) {
                    showToastMessage("请填写关键字");
                } else {
                    presenter.getpatient(pageIndex, name, "3");
                }

                break;
        }
    }

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_search);
    }

    @Override
    public void getpatientfail() {
        showToastMessage("网络连接失败，请检查您的网络设置");
        listView.onRefreshFinish();
    }

    @Override
    public void getpatientsucced(List<Patient> doctors) {
        if (doctors != null && doctors.size() > 0) {
            if (pageIndex == 1) {
                this.patients.clear();
                adapter.setData(doctors);
                listView.onRefreshFinish();
            }else{
                if (doctors.size() < ConstantValue.LOAD_MESSAGE_COUNT) {
                    adapter.addData(doctors);
                    listView.showNoMoreData();
                } else {
                    adapter.addData(doctors);
                    listView.hideNoMoreData();
                }
            }
            adapter.notifyDataSetChanged();
        } else {
            if (pageIndex == 1) {
                listView.onRefreshFinish();
            }else{
                listView.showNoMoreData();
                adapter.notifyDataSetChanged();
            }
        }
    }
}
